package com.guli.gulimall.gulimallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.guli.gulimall.gulimallproduct.dao.AttrAttrgroupRelationDao;
import com.guli.gulimall.gulimallproduct.entity.AttrEntity;
import com.guli.gulimall.gulimallproduct.service.AttrAttrgroupRelationService;
import com.guli.gulimall.gulimallproduct.service.AttrService;
import com.guli.gulimall.gulimallproduct.service.CategoryService;
import com.guli.gulimall.gulimallproduct.vo.AttrGroupRelationVo;
import com.guli.gulimall.gulimallproduct.vo.AttrGroupWithAttrsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guli.gulimall.gulimallproduct.entity.AttrGroupEntity;
import com.guli.gulimall.gulimallproduct.service.AttrGroupService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;



/**
 * 属性分组
 *
 * @author zouzhi
 * @email 1245268075@qq.com
 * @date 2022-04-03 10:19:09
 */
@RestController
@RequestMapping("gulimallproduct/attrgroup")
@Api(value = "AttrGroupController接口",tags = "商品属性分分组")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
      //gulimallproduct/attrgroup/{attr_group_id}/attr/relation
    /**
     * 获取属性分组id关联到的列表
     */
    @GetMapping("/{attr_group_id}/attr/relation")
    @ApiOperation(value = "根据商品分组id查询所有关联此分组id的商品",notes = "可参考pms_attr_attrgroup_relation表和pms_attr表")
    public  R attrRelation(@PathVariable Long attr_group_id){
        List<AttrEntity> attrEntityList =attrService.getRelationAttr(attr_group_id);
        return  R.ok().put("data",attrEntityList);
    }

    /**
     * 获取属性id没有关联的数据
     * @param
     * @return
     */
    @GetMapping("/{attr_group_id}/noattr/relation")
    @ApiOperation(value = "根据商品分组id查询所有关联此分组id的商品",notes = "可参考pms_attr_attrgroup_relation表和pms_attr表")
    public  R noattrRelation(@PathVariable Long attr_group_id,@RequestParam(required = false) Map<String, Object> params){
        PageUtils pageUtils =attrService.getNoRelationAttr(attr_group_id,params);
        return  R.ok().put("data",pageUtils);
    }

    @ApiOperation(value = "根据商品分组id和商品属性id删除关联关系,批量删除",tags = "根据商品分组id和商品属性id删除关联关系,参考pms_attr_attrgroup_relation表")
    @PostMapping("/attr/relation/delete")
    public  R deleteAttrRelationbyAttrIdAttrGroupId(@RequestBody  AttrGroupRelationVo[] vos){
        attrService.deleRelation(vos);
        System.out.println(vos);
        return  R.ok();
    }
    /**
     * 列表
     */
    @GetMapping("/list/{catId}")
    @ApiOperation("属性分组接口")
    public R list(@RequestParam(required = false) Map<String, Object> params, @PathVariable Long catId){
        PageUtils page = this.attrGroupService.queryPage(params,catId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation("根据属性分组Id查询属性的父属性和本属性")
    @GetMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){

		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		if(null==attrGroup)return R.error().put("attrGroup","没有查询到信息");
        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);
        System.out.println(Arrays.toString(path));
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }
    @ApiOperation("添加属性关联")
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> relationVos){
        System.out.println(relationVos);
        attrAttrgroupRelationService.saveBatch(relationVos);
        return  R.ok();
    }
//    /product/attrgroup/{catelogId}/withattr
    @ApiOperation("通过分组id获取当前分组信息及其关联的商品分类信息")
    @GetMapping("{catelogId}/withattr")
    public  R getAttrGroupWithAttrs(@PathVariable Long catelogId){
        //1/查出当前分类下的所有属性分组
        //2 查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> attrGroupWithAttrs = this.attrGroupService.getAttrGroupWithAttrsByCatelogID(catelogId);
        return  R.ok().put("data",attrGroupWithAttrs);
    }
    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
